package com.avic.rong.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;

import com.avic.rong.MainActivity;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.questionOrAnswer.activity.AnswerDetailsActivity;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.microquation.linkedme.android.LinkedME;
import com.microquation.linkedme.android.indexing.LMUniversalObject;
import com.microquation.linkedme.android.util.LinkProperties;
import com.orhanobut.logger.Logger;

import java.util.HashMap;

/**
 * <p>中转页面</p>
 * <p>
 * Created by LinkedME06 on 16/11/17.
 */

public class MiddleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Toast.makeText(this, "MiddleActivity 被调用了", Toast.LENGTH_SHORT).show();
        //获取与深度链接相关的值
        LinkProperties linkProperties = getIntent().getParcelableExtra(LinkedME.LM_LINKPROPERTIES);
        LMUniversalObject lmUniversalObject = getIntent().getParcelableExtra(LinkedME.LM_UNIVERSALOBJECT);
        //LinkedME SDK初始化成功，获取跳转参数，具体跳转参数在LinkProperties中，和创建深度链接时设置的参数相同；
        if (linkProperties != null) {
            Log.i("LinkedME-Demo", "Channel " + linkProperties.getChannel());
            Log.i("LinkedME-Demo", "control params " + linkProperties.getControlParams());
            Log.i("LinkedME-Demo", "link(深度链接) " + linkProperties.getLMLink());
            Log.i("LinkedME-Demo", "是否为新安装 " + linkProperties.isLMNewUser());
            //获取自定义参数封装成的hashmap对象
            HashMap<String, String> hashMap = linkProperties.getControlParams();

            //获取传入的参数
            String shareType = hashMap.get(ConstantsUtils.SHARE_TYPE);
            String shareId = hashMap.get(ConstantsUtils.SHARE_ID);
            String typeFlag = hashMap.get(ConstantsUtils.TYPE_FLAG);
            Logger.i("------------------shareType:" + shareType + "---shareId:" + shareId + "------typeFlag:" + typeFlag + "-----");
            if (lmUniversalObject != null) {
                Logger.i("-------------------------------lmuniversal" + lmUniversalObject.toString());
            }
            //1:文章 2:商品 3：培训  4:回答 5:问题  6,首页  7.商城首页  8.培训首页 9.世界标准日
            if (!TextUtils.isEmpty(shareType)) {
                if (shareType.equals("1")) {
                    if (!TextUtils.isEmpty(typeFlag) && "5".equals(typeFlag)) {
                        startActivity(VideoDetailActivity.getIntent(MiddleActivity.this, shareId));
                    } else {
                        startActivity(ArticleDetailActivity.getIntent(MiddleActivity.this, shareId));
                    }
                } else if (shareType.equals("2")) {
                    startActivity(DetailProductActivity.getIntent(MiddleActivity.this, shareId));
                } else if (shareType.equals("3")) {
                    startActivity(CourseDetailActivity.getIntent(MiddleActivity.this, shareId, null, null));
                } else if (shareType.equals("4")) {
                    startActivity(AnswerDetailsActivity.queryIntent(MiddleActivity.this, shareId));
                } else if (shareType.equals("5")) {
                    startActivity(QuestionDetailsActivity.queryIntent(MiddleActivity.this, shareId));
                } else if (shareType.equals("6")) {
                    startActivity(MainActivity.queryIntent(MiddleActivity.this, 0));
                } else if (shareType.equals("7")) {
                    startActivity(MainActivity.queryIntent(MiddleActivity.this, 1));
                } else if (shareType.equals("8")) {
                    startActivity(MainActivity.queryIntent(MiddleActivity.this, 0, "培训"));
                } else if(shareType.equals("9")){
                    startActivity(MainActivity.queryIntent(MiddleActivity.this, 0, "世界标准日"));
                }
            }


            //清除跳转数据，该方法理论上不需要调用，因Android集成方式各种这样，若出现重复跳转的情况，可在跳转成功后调用该方法清除参数
            LinkedME.getInstance().clearSessionParams();
        }

//        if (lmUniversalObject != null) {
//            Log.i("LinkedME-Demo", "title " + lmUniversalObject.getTitle());
//            Log.i("LinkedME-Demo", "control " + linkProperties.getControlParams());
//            Log.i("ContentMetaData", "metadata " + lmUniversalObject.getMetadata());
//        }
        finish();
    }

}
